package uk.ac.lkl.lpp.ui;



import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.ImageIcon;

import org.jdesktop.jdic.desktop.Desktop;

import uk.ac.lkl.common.util.Linkable;
import uk.ac.lkl.common.util.Resources;



/**
 * A button for a hyperlink. click the button & follow the link. 
 * <p><b>Question:</b> shouldn't this be in common.ui?</p>
 * <ul>
 * <li><em>5 Nov, 2007 (yish)</em>: added support for button text & arbitrary icons.</li> 
 * </ul
 * @author Darren Pearce
 *
 */
public class HyperlinkButton extends JButton {

    private static HyperlinkEditor linkEditor = new HyperlinkEditor();
    
    private static String ICON_LOCATION = "gfx/link.gif";

    private static ImageIcon linkIcon = Resources.getIcon(ICON_LOCATION);

    /*
    static {
        ClassLoader loader = HyperlinkButton.class.getClassLoader();
        URL imageURL = loader.getResource("gfx/link.gif");
        linkIcon = new ImageIcon(imageURL);
        linkEditor = new HyperlinkEditor();        
    };
	*/
    
    private Linkable linkable;

    private boolean editing;

    private ActionListener linkEditorActionListener = new ActionListener() {

        // can't remove action listener here since get
        // ConcurrentModificationException
        public void actionPerformed(ActionEvent e) {
            processLinkEditorActionPerformed();
        }
    };

    public HyperlinkButton(Linkable linkable, ImageIcon linkIcon, String toolTipText) {
        super(linkIcon); 
        setup(linkable,toolTipText);
    }
    
    public HyperlinkButton(Linkable linkable, String label, String toolTipText) {
        super(label); 
        setup(linkable,toolTipText);
    }


    public HyperlinkButton(Linkable linkable) {
    	this(linkable, linkIcon, null);
    }

    protected void setup(Linkable linkable,String toolTipText){
        this.linkable = linkable;
        setBorder(BorderFactory.createEmptyBorder());
        setContentAreaFilled(false);
        setOpaque(false);        
        editing = false;
        addActionListeners();
        if (toolTipText != null) {setToolTipText(toolTipText);}
        else {updateToolTipText();}
    }
    
    private void addActionListeners() {
        addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                try {
                    if (((e.getModifiers() & ActionEvent.SHIFT_MASK) >= 1) || 
                    		linkable.getLink().length() < 4) {
                        linkEditor.show(HyperlinkButton.this);
                        linkEditor.setLinkText(linkable.getLink());                        
                        editing = true;
                    }
                    else {
                        Desktop.browse(new URL(linkable.getLink()));
                    }
                }
                catch (Exception exc) {
                    exc.printStackTrace();
                }
            }
        });

        linkEditor.addActionListener(linkEditorActionListener);
    }


    private void processLinkEditorActionPerformed() {
        if (!editing)
            return;

        updateLinkable();
        updateToolTipText();
        
        editing = false;
    }


    private void updateLinkable() {
        linkable.setLink(linkEditor.getLinkText());
    }


    private void updateToolTipText() {
    	if (linkable.getLink() == null || linkable.getLink().length() < 1) {
    		setToolTipText(null);
    	} else {
    		setToolTipText(linkable.getLink());
    	}
    }
};